package ar.com.agrobots;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.JPanel;

import ar.com.agrobots.red.Camino;
import ar.com.agrobots.red.Conexion;
import ar.com.agrobots.red.Nodo;
import ar.com.agrobots.red.Red;




public class Interfaz extends JPanel {
	  private Camino CaminoOptimo;
	  private Red red;
	  private  Map<String,Point> MapaNodos = new HashMap<String,Point> ();
	  
	  // Declaracion de constantes
	  private static int margen = 10;
	  private static int NodeInterspace = 120;
	  private static int CantNodosFila = 5;
	  private static int CircleDiam = 20;
	  private static int XMargin = 30;
	  private static int YMargin = 50;
	  private static int RawSpace = 70;
	    //Colores
	  private static int verde = 234423;
	  private static int azul = 234423;//412;
	  
	  public Interfaz() {
		  super();
	  }
	  
	  public Interfaz(Red red,Camino caminoOptimo){
		  CaminoOptimo = caminoOptimo;
		  this.red = red;
		  FillMapaNodos ();
		  
	  }
      /* FillMapaNodos llena el mapa donde se guardan las coordenadas de cada nodo con la red
       * que se levanto del XML
       */
	  void FillMapaNodos (){
		  int XPos,YPos,j=0,Offset;
		  XPos = XMargin;
		  Iterator<Nodo> iterator = red.getNodos().iterator();
		  XPos = XMargin ;	 
	      YPos = YMargin;
	      //Ubico al nodo I para que se ubique al principio de la primera fila
	      Nodo nodoI = (Nodo) iterator.next();
	      MapaNodos.put(nodoI.getName(),new Point (XPos ,YPos ));
	      Offset = XPos + NodeInterspace;
		  for (; iterator.hasNext();j++) {
				Nodo nodo = (Nodo) iterator.next();	
				XPos = NodeInterspace* (j % CantNodosFila ) + Offset;	 
		    	YPos = (int) (RawSpace* Math.floor (j / CantNodosFila ) +YMargin);
				MapaNodos.put(nodo.getName(),new Point (XPos ,YPos ));						
		  } 
		  //Reubico el nodo F para que quede al final de la primera fila
		  MapaNodos.get ("F").x = NodeInterspace* CantNodosFila  + Offset;
		  MapaNodos.get ("F").y = YMargin;
	  }
	  
	  
	  void DibujarRed (Graphics g){
		  int XPos,YPos,NroNodo = 0,j=0;
		  Graphics2D g2 = (Graphics2D)g;		  
		  XPos = XMargin;
		  for (Iterator<Nodo> iterator = red.getNodos().iterator(); iterator.hasNext();j++) {
			  Nodo nodo = (Nodo) iterator.next();
			  //Busco las coordenadas del nodo
			  Point pointNodoStart = MapaNodos.get (nodo.getName());
			 // Dibujo el nodo centrado
			  g2.setColor(new Color (azul));
	    	  g2.fillOval(pointNodoStart.x - CircleDiam / 2, pointNodoStart.y - CircleDiam / 2, CircleDiam, CircleDiam);
	    	  //Dibujo el nombre
	    	  g2.drawBytes(nodo.getName().getBytes(), 0, nodo.getName().length(),pointNodoStart.x - CircleDiam / 2  , pointNodoStart.y  - CircleDiam / 2 );

	    	  for (Iterator<Conexion> iteratorCnx = nodo.getConexiones().iterator(); iteratorCnx.hasNext();) {
				  Conexion Cnx = (Conexion) iteratorCnx.next();
				  Point pointNodoEnd = MapaNodos.get (Cnx.getNodoDestino());
				  //Dibujo la linea que conecta ambos nodos
				  g2.setColor(new Color (azul));
				  g2.drawLine(pointNodoStart.x , pointNodoStart.y,pointNodoEnd.x , pointNodoEnd.y);
				  //Dibujo los pesos
				  String Peso = String.valueOf(Cnx.getPeso());
				  g2.setColor(new Color (0));
		    	  g2.drawBytes(Peso.getBytes(), 0, Peso.length(),(pointNodoStart.x + pointNodoEnd.x) / 2 , (pointNodoStart.y + pointNodoEnd.y) / 2 );
	    	  }
					
		  }
		  
	  }

	  public void paintComponent( Graphics g ) {
		int XPos,YPos,NroNodo = 0;
	    super.paintComponent( g );
	    Graphics2D g2 = (Graphics2D)g;
	   
	    if (this.red != null && this.CaminoOptimo != null) {
	    	FillMapaNodos();
		    g2.setStroke (new BasicStroke(5));
		    g2.setColor(new Color (verde));   
			Iterator<Nodo> iterator = CaminoOptimo.getOrdenNodos().iterator();
		    Nodo nodo = (Nodo) iterator.next();
		    Point pointNodoAnt = MapaNodos.get (nodo.getName());	    
			for (; iterator.hasNext();) {
				nodo = (Nodo) iterator.next();
				Point pointNodo = MapaNodos.get (nodo.getName());
				g2.setColor(new Color (3123312));	    		
				g2.fillOval(pointNodo.x - CircleDiam / 2, pointNodo.y - CircleDiam / 2, CircleDiam, CircleDiam);
				g2.drawLine(pointNodoAnt.x , pointNodoAnt.y,pointNodo.x , pointNodo.y);
				pointNodoAnt = pointNodo;			
			}
			g2.setStroke (new BasicStroke(1));
		    g2.setColor(new Color (0));
			DibujarRed (g);
			
	    }
	  }
	public Camino getCaminoOptimo() {
		return CaminoOptimo;
	}
	public void setCaminoOptimo(Camino caminoOptimo) {
		CaminoOptimo = caminoOptimo;
	}
	public Red getRed() {
		return red;
	}
	public void setRed(Red red) {
		this.red = red;
	}    
}

